package com.iocollege.tinyframe.mapper.system;

import com.iocollege.tinyframe.entity.system.SysDept;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 部门表 Mapper 接口
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
public interface SysDeptMapper extends BaseMapper<SysDept> {

    /**
     * 查询部门管理数据
     *
     * @param dept 部门信息
     * @return 部门信息集合
     */
    @Select("<script> select * from sys_dept d where d.del_flag = '0'\n" +
            "        <if test=\"parentId != null and parentId != 0\">\n" +
            "\t\t\tAND parent_id = #{parentId}\n" +
            "\t\t</if>\n" +
            "\t\t<if test=\"deptName != null and deptName != ''\">\n" +
            "\t\t\tAND dept_name like concat('%', #{deptName}, '%')\n" +
            "\t\t</if>\n" +
            "\t\t<if test=\"status != null and status != ''\">\n" +
            "\t\t\tAND status = #{status}\n" +
            "\t\t</if>\n" +
            "\t\torder by d.parent_id, d.order_num</script>")
    public List<SysDept> selectDeptList(SysDept dept);



    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId 角色ID
     * @param deptCheckStrictly 部门树选择项是否关联显示
     * @return 选中部门列表
     */
    @Select("<script>select d.dept_id\n" +
            "\t\tfrom sys_dept d\n" +
            "            left join sys_role_dept rd on d.dept_id = rd.dept_id\n" +
            "        where rd.role_id = #{roleId}\n" +
            "            <if test=\"deptCheckStrictly\">\n" +
            "              and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})\n" +
            "            </if>\n" +
            "\t\torder by d.parent_id, d.order_num</script>")
    public List<Integer> selectDeptListByRoleId(@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly);



    /**
     * 根据ID查询所有子部门
     *
     * @param deptId 部门ID
     * @return 部门列表
     */
    @Select("select * from sys_dept where find_in_set(#{deptId}, ancestors)")
    public List<SysDept> selectChildrenDeptById(Long deptId);



    /**
     * 根据ID查询所有子部门（正常状态）
     *
     * @param deptId 部门ID
     * @return 子部门数
     */
    @Select("select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)")
    public int selectNormalChildrenDeptById(Long deptId);



    /**
     * 是否存在子节点
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Select("select count(1) from sys_dept\n" +
            "\t\twhere del_flag = '0' and parent_id = #{deptId} limit 1")
    public int hasChildByDeptId(Long deptId);




    /**
     * 查询部门是否存在用户
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Select("select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'")
    public int checkDeptExistUser(Long deptId);


    /**
     * 校验部门名称是否唯一
     *
     * @param deptName 部门名称
     * @param parentId 父部门ID
     * @return 结果
     */
    @Select("select * from sys_dept where dept_name=#{deptName} and parent_id = #{parentId} limit 1")
    public SysDept checkDeptNameUnique(@Param("deptName") String deptName, @Param("parentId") Long parentId);



    /**
     * 修改所在部门的父级部门状态
     *
     * @param dept 部门
     */
    @Update("<script> update sys_dept\n" +
            " \t    <set>\n" +
            " \t        <if test=\"status != null and status != ''\">status = #{status},</if>\n" +
            " \t        <if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>\n" +
            " \t        update_time = sysdate()\n" +
            "        </set>\n" +
            " \t    where dept_id in (${ancestors})</script>")
    public void updateDeptStatus(SysDept dept);



    /**
     * 修改子元素关系
     *
     * @param depts 子元素
     * @return 结果
     */
    @Update("<script>  update sys_dept set ancestors =\n" +
            "\t    <foreach collection=\"depts\" item=\"item\" index=\"index\"\n" +
            "\t        separator=\" \" open=\"case dept_id\" close=\"end\">\n" +
            "\t        when #{item.deptId} then #{item.ancestors}\n" +
            "\t    </foreach>\n" +
            "\t    where dept_id in\n" +
            "\t    <foreach collection=\"depts\" item=\"item\" index=\"index\"\n" +
            "\t        separator=\",\" open=\"(\" close=\")\">\n" +
            "\t        #{item.deptId}\n" +
            "\t    </foreach></script>")
    public int updateDeptChildren(@Param("depts") List<SysDept> depts);


}
